package cn.itsource.pethome.pet.service.impl;

import cn.itsource.pethome.base.service.impl.BaseServiceImpl;
import cn.itsource.pethome.org.domain.Employee;
import cn.itsource.pethome.pet.domain.Pet;
import cn.itsource.pethome.pet.domain.PetDetail;
import cn.itsource.pethome.pet.domain.PetOnlineAuditLog;
import cn.itsource.pethome.pet.mapper.PetDetailMapper;
import cn.itsource.pethome.pet.mapper.PetMapper;
import cn.itsource.pethome.pet.mapper.PetOnlineAuditLogMapper;
import cn.itsource.pethome.pet.service.IPetService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zcj
 * @since 2022-10-23
 */
@Service
public class PetServiceImpl extends BaseServiceImpl<Pet> implements IPetService {
    @Autowired
    private PetDetailMapper petDetailMapper;
    @Autowired
    private PetMapper petMapper;
    @Autowired
    private PetOnlineAuditLogMapper petOnlineAuditLogMapper;

    @Override
    public void update(Pet pet) {
        super.update(pet);
    }

    @Override
    public void delete(Long id) {
        super.delete(id);
    }

    @Override
    @Transactional
    public void insert(Pet pet, Employee loginUser) {
        //做什么事情
        //继续初始化
        pet.setShopId(loginUser.getShop_id());
        //先保存 pet
        super.insert(pet);
        PetDetail detail = pet.getDetail();
        detail.setPetId(pet.getId());
        //在保存petDetail
        petDetailMapper.insert(detail);
    }

    /**
     * 下架
     * @param ids
     */
    @Override
    @Transactional
    public void offsale(List<Long> ids) {
       //下架时间
       //修改状态
        Date date = new Date();
        petMapper.offsale(date,ids);
        List<PetOnlineAuditLog> lists = new ArrayList<>();
        for (Long id : ids) {
            PetOnlineAuditLog petOnlineAuditLog = new PetOnlineAuditLog();
            petOnlineAuditLog.setAuditTime(date);
            petOnlineAuditLog.setPetId(id);
            petOnlineAuditLog.setState(1);
            petOnlineAuditLog.setNote("下架成功");
            lists.add(petOnlineAuditLog);
        }
        petOnlineAuditLogMapper.batchAdd(lists);
    }

    @Override
    public Map<String,Object> onsale(List<Long> ids) {
        Map<String,Object> map = new HashMap<>();
        List<String> names = new ArrayList<>();
        //先去审核
        for (Long id : ids) {
            //日志记录
            PetOnlineAuditLog petOnlineAuditLog = new PetOnlineAuditLog();
            Date date = new Date();
            petOnlineAuditLog.setAuditTime(date);
            petOnlineAuditLog.setPetId(id);
            //获取当前上架物品
            Pet pet = petMapper.queryOne(id);
//            if(BaiduAuditUtils.baiduTextVerify(pet.getName())){//是否审核通过
            if(true){//是否审核通过
                pet.setState(1L);
                pet.setOnsaletime(date);
                petOnlineAuditLog.setState(1);
                petOnlineAuditLog.setNote("审核成功");
                petMapper.update(pet);
            }else {
                //日志记录
                petOnlineAuditLog.setState(0);
                petOnlineAuditLog.setNote("审核失败");
                //记录错误的
                names.add(pet.getName());
            }
            petOnlineAuditLogMapper.insert(petOnlineAuditLog);
        }
        map.put("names",names);
        if(names.size() == 0){
            return null;
        }
        return map;
    }
}
